﻿using UnityEngine;
using System.Collections;

public class CharacterAnimation : MonoBehaviour {

	public Animator anim;
	private Vector2 facingDirection;
	private SpriteRenderer spriteRenderer;
	private bool characterIsMoving;

	public Sprite IdleUp;
	public Sprite IdleDown;
	public Sprite IdleRight;
	public Sprite IdleLeft;



	// Use this for initialization
	void Start () 
	{
		anim = GetComponent<Animator>();
		spriteRenderer = GetComponent<SpriteRenderer>();
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (!characterIsMoving) //ESTOY EN IDLE
		{
			anim.SetBool("WalkLeft", false);

			if (facingDirection.y == 0)
			{
				//moving in X axis
				if (facingDirection.x > 0)
				{
					spriteRenderer.sprite = IdleRight;			
				}
				else 
				{
					spriteRenderer.sprite = IdleLeft;
				}
			}
			else if (facingDirection.x == 0)
			{
				if (facingDirection.y > 0)
				{
					spriteRenderer.sprite = IdleUp;
				}
				else
				{
					spriteRenderer.sprite = IdleDown;
				}
			}	

			return;
		}

		if (facingDirection.y == 0)
		{
			//moving in X axis
			if (facingDirection.x > 0)
			{

			}
			else 
			{
				anim.SetBool("WalkLeft", true);
			}
		}
		else if (facingDirection.x == 0)
		{
			if (facingDirection.y > 0)
			{

			}
			else
			{

			}
		}	
	}

	public void SetPlayerFacingDirection(Vector2 direction)
	{
		facingDirection = direction;
	}

	public void SetCharacterMoving(bool moving)
	{
		characterIsMoving = moving;
	}
}
